package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.bedroomglory.BedroomGloryAddDto;
import com.teacher.pojo.dto.bedroomglory.BedroomGloryGetDto;
import com.teacher.pojo.dto.bedroomglory.BedroomGloryUpdateDto;
import com.teacher.pojo.entity.BedroomGlory;
import com.teacher.pojo.vo.bedroomglory.BedroomGloryGetVo;
import com.teacher.service.IBedroomGloryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 寝室荣誉 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/pc/bedroom-glory")
@Api(tags = "寝室荣誉")
public class BedroomGloryController {

    @Autowired
    private IBedroomGloryService iBedroomGloryService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加寝室荣誉
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加寝室荣誉 bedroomGlory_add")
    @PostMapping
    @PreAuthorize("hasAuthority('bedroomGlory_add')")
    public CommonResult<String> add(@RequestBody @Validated BedroomGloryAddDto dto) {
        BedroomGlory bedroomGlory = new BedroomGlory();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, bedroomGlory);
        if (iBedroomGloryService.save(bedroomGlory)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新寝室荣誉
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新寝室荣誉 bedroomGlory_update")
    @PutMapping
    @PreAuthorize("hasAuthority('bedroomGlory_update')")
    public CommonResult<String> update(@RequestBody @Validated BedroomGloryUpdateDto dto) {
        BedroomGlory bedroomGlory = new BedroomGlory();
        BeanUtils.copyProperties(dto, bedroomGlory);
        if (iBedroomGloryService.updateById(bedroomGlory)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除寝室荣誉
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除寝室荣誉 bedroomGlory_delete")
    @PreAuthorize("hasAuthority('bedroomGlory_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iBedroomGloryService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询寝室荣誉
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询寝室荣誉 bedroomGlory_get")
    @PreAuthorize("hasAuthority('bedroomGlory_get')")
    public CommonResult<IPage<BedroomGloryGetVo>> getPage(BedroomGloryGetDto dto) {
        return iBedroomGloryService.getPage(dto);
    }

    /**
     * 导入寝室荣誉
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入寝室荣誉 bedroomGlory_upload")
    @PreAuthorize("hasAuthority('bedroomGlory_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iBedroomGloryService.upload(file);
    }
}

